/* jQuery plugin themeswitcher
---------------------------------------------------------------------*/

$.themeswitcher = {
	regional : {
		'': {
			initialText: 'Switch Theme',
			buttonPreText: 'Theme: ',
			themes : [{dir:'ui-lightness',
					icon:'theme_30_ui_light.png',
					title:'UI Lightness',
					name:'UI Lightness'
				},
				{dir:'ui-darkness',
					icon:'theme_30_ui_dark.png',
					title:'UI Darkness',
					name:'UI Darkness'
				},
				{dir:'smoothness',
					icon:'theme_30_smoothness.png',
					title:'Smoothness',
					name:'Smoothness'
				},
				{dir:'start',
					icon:'theme_30_start_menu.png',
					title:'Start',
					name:'Start'
				},
				{dir:'redmond',
					icon:'theme_30_windoze.png',
					title:'Redmond',
					name:'Redmond'
				},
				{dir:'sunny',
					icon:'theme_30_sunny.png',
					title:'Sunny',
					name:'Sunny'
				},
				{dir:'overcast',
					icon:'theme_30_overcast.png',
					title:'Overcast',
					name:'Overcast'
				},
				{dir:'le-frog',
					icon:'theme_30_le_frog.png',
					title:'Le Frog',
					name:'Le Frog'
				},
				{dir:'flick',
					icon:'theme_30_flick.png',
					title:'Flick',
					name:'Flick'
				},
				{dir:'pepper-grinder',
					icon:'theme_30_pepper_grinder.png',
					title:'Pepper Grinder',
					name:'Pepper Grinder'
				},
				{dir:'eggplant',
					icon:'theme_30_eggplant.png',
					title:'Eggplant',
					name:'Eggplant'
				},
				{dir:'dark-hive',
					icon:'theme_30_dark_hive.png',
					title:'Dark Hive',
					name:'Dark Hive'
				},
				{dir:'cupertino',
					icon:'theme_30_cupertino.png',
					title:'Cupertino',
					name:'Cupertino'
				},
				{dir:'south-street',
					icon:'theme_30_south_street.png',
					title:'South St',
					name:'South St'
				},
				{dir:'blitzer',
					icon:'theme_30_blitzer.png',
					title:'Blitzer',
					name:'Blitzer'
				},
				{dir:'humanity',
					icon:'theme_30_humanity.png',
					title:'Humanity',
					name:'Humanity'
				},
				{dir:'hot-sneaks',
					icon:'theme_30_hot_sneaks.png',
					title:'Hot Sneaks',
					name:'Hot Sneaks'
				},
				{dir:'excite-bike',
					icon:'theme_30_excite_bike.png',
					title:'Excite Bike',
					name:'Excite Bike'
				},
				{dir:'vader',
					icon:'theme_30_black_matte.png',
					title:'Vader',
					name:'Vader'
				},
				{dir:'dot-luv',
					icon:'theme_30_dot_luv.png',
					title:'Dot Luv',
					name:'Dot Luv'
				},
				{dir:'mint-choc',
					icon:'theme_30_mint_choco.png',
					title:'Mint Choc',
					name:'Mint Choc'
				},
				{dir:'black-tie',
					icon:'theme_30_black_tie.png',
					title:'Black Tie',
					name:'Black Tie'
				},
				{dir:'trontastic',
					icon:'theme_30_trontastic.png',
					title:'Trontastic',
					name:'Trontastic'
				},
				{dir:'swanky-purse',
					icon:'theme_30_swanky_purse.png',
					title:'Swanky Purse',
					name:'Swanky Purse'
				}]}
	}
};

$.fn.themeswitcher = function(settings){
	var options = jQuery.extend({
		loadTheme: null,
		initialText: '',
		width: 150,
		height: 200,
		buttonPreText: '',
		closeOnSelect: true,
		buttonHeight: 14,
		cookieName: 'jquery-ui-theme',
		themeCodeBase: 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes',
		galleryImagesBase: 'http://static.jquery.com/ui/themeroller/images/themeGallery',
		toolImagesBase: 'http://jqueryui.com/themeroller/themeswitchertool/images',
		themes : [],
		onOpen: function(){},
		onClose: function(){},
		onSelect: function(){}
		
	}, $.themeswitcher.regional[''], settings);
	
	$(this).html('').next().remove();
	
	//markup 
	var button = $('<a href="#" class="jquery-ui-themeswitcher-trigger">'+
		'<span class="jquery-ui-themeswitcher-icon"></span>'+
		'<span class="jquery-ui-themeswitcher-title">'+ options.initialText +'</span></a>');
	
	var template='\n		<li><a href="'+options.themeCodeBase+'/{dir}/jquery-ui.css">'+
	'\n			<img src="'+options.galleryImagesBase+'/{icon}" alt="{title}" title="{title}" />'+
	'\n			<input type="hidden" value="{name}"/>'+
	'\n			<span class="themeName">{title}</span></a></li>';
	
	var themes = '';
	for (var i in options.themes) {
		themes += template.replace(/\{dir\}/, options.themes[i].dir).
			replace(/\{icon\}/, options.themes[i].icon).
			replace(/\{name\}/, options.themes[i].name).
			replace(/\{title\}/g, options.themes[i].title);
	}
	//alert(themes);
	
	var switcherpane = $('<div class="jquery-ui-themeswitcher">'+
		'<div id="themeGallery">	<ul>'+themes+'	</ul></div></div>').find('div').removeAttr('id');
	
	//button events
	button.click(
		function(){
			if(switcherpane.is(':visible')){ switcherpane.spHide(); }
			else{ switcherpane.spShow(); }
					return false;
		}
	);
	
	//menu events (mouseout didn't work...)
	switcherpane.hover(
		function(){},
		function(){if(switcherpane.is(':visible')){$(this).spHide();}}
	);
 
	//show/hide panel functions
	$.fn.spShow = function(){ $(this).css({top: button.offset().top + options.buttonHeight + 6, left: button.offset().left}).slideDown(50); button.css(button_active); options.onOpen(); }
	$.fn.spHide = function(){ $(this).slideUp(50, function(){options.onClose();}); button.css(button_default); }
	
		
	/* Theme Loading
	---------------------------------------------------------------------*/
	switcherpane.find('a').click(function(){
		updateCSS( $(this).attr('href') );
		button.find('.jquery-ui-themeswitcher-title').text( options.buttonPreText + $(this).find('span').text() );
		$.cookie(options.cookieName, $(this).find('input').val());
		options.onSelect(this);
		if(options.closeOnSelect && switcherpane.is(':visible')){ switcherpane.spHide(); }
		return false;
	});
	
	//function to append a new theme stylesheet with the new style changes
	function updateCSS(locStr){
		var cssLink = $('<link href="'+locStr+'" type="text/css" rel="Stylesheet" class="ui-theme" />');
		$("head").append(cssLink);
		
		if( $("link.ui-theme").size() > 3){
			$("link.ui-theme:first").remove();
		}	
	}	
	
	/* Inline CSS 
	---------------------------------------------------------------------*/
	var button_default = {
		fontFamily: 'Trebuchet MS, Verdana, sans-serif',
		fontSize: '11px',
		color: '#666',
		background: '#eee url('+options.toolImagesBase+'/buttonbg.png) 50% 50% repeat-x',
		border: '1px solid #ccc',
		'-moz-border-radius': '6px',
		'-webkit-border-radius': '6px',
		textDecoration: 'none',
		padding: '3px 3px 3px 8px',
		width: options.width - 11,//minus must match left and right padding 
		display: 'block',
		height: options.buttonHeight,
		outline: '0'
	};
	var button_hover = {
		'borderColor':'#bbb',
		'background': '#f0f0f0',
		cursor: 'pointer',
		color: '#444'
	};
	var button_active = {
		color: '#aaa',
		background: '#000',
		border: '1px solid #ccc',
		borderBottom: 0,
		'-moz-border-radius-bottomleft': 0,
		'-webkit-border-bottom-left-radius': 0,
		'-moz-border-radius-bottomright': 0,
		'-webkit-border-bottom-right-radius': 0,
		outline: '0'
	};
	
	
	
	//button css
	button.css(button_default)
	.hover(
		function(){ 
			$(this).css(button_hover); 
		},
		function(){ 
		 if( !switcherpane.is(':animated') && switcherpane.is(':hidden') ){	$(this).css(button_default);  }
		}	
	)
	.find('.jquery-ui-themeswitcher-icon').css({
		float: 'right',
		width: '16px',
		height: '16px',
		background: 'url('+options.toolImagesBase+'/icon_color_arrow.gif) 50% 50% no-repeat'
	});	
	//pane css
	switcherpane.css({
		position: 'absolute',
		float: 'left',
		fontFamily: 'Trebuchet MS, Verdana, sans-serif',
		fontSize: '12px',
		background: '#000',
		color: '#fff',
		padding: '8px 3px 3px',
		border: '1px solid #ccc',
		'-moz-border-radius-bottomleft': '6px',
		'-webkit-border-bottom-left-radius': '6px',
		'-moz-border-radius-bottomright': '6px',
		'-webkit-border-bottom-right-radius': '6px',
		borderTop: 0,
		zIndex: 999999,
		width: options.width-6//minus must match left and right padding
	})
	.find('ul').css({
		listStyle: 'none',
		margin: '0',
		padding: '0',
		overflow: 'auto',
		height: options.height
	}).end()
	.find('li').hover(
		function(){ 
			$(this).css({
				'borderColor':'#555',
				'background': 'url('+options.toolImagesBase+'/menuhoverbg.png) 50% 50% repeat-x',
				cursor: 'pointer'
			}); 
		},
		function(){ 
			$(this).css({
				'borderColor':'#111',
				'background': '#000',
				cursor: 'auto'
			}); 
		}
	).css({
		width: options.width-30,
		height: '',
		padding: '2px',
		margin: '1px',
		border: '1px solid #111',
		'-moz-border-radius': '4px',
		clear: 'left',
		float: 'left'
	}).end()
	.find('a').css({
		color: '#aaa',
		textDecoration: 'none',
		float: 'left',
		width: '100%',
		outline: '0'
	}).end()
	.find('img').css({
		float: 'left',
		border: '1px solid #333',
		margin: '0 2px'
	}).end()
	.find('.themeName').css({
		float: 'left',
		margin: '3px 0'
	}).end();
	
 
 
	$(this).append(button);
	switcherpane.insertAfter($(this));
	switcherpane.hide();
	if( $.cookie(options.cookieName) || options.loadTheme ){
		var themeName = $.cookie(options.cookieName) || options.loadTheme;
		switcherpane.find('a[name="'+ themeName +'"]').trigger('click');
	}
};

/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};